package offer22

import node.ListNode
import node.ListNodeManager

class Solution {
    companion object {
        @JvmStatic
        fun main(s: Array<String>) {
            var nums: IntArray = intArrayOf(1, 2, 3, 4, 5)
            val head = ListNodeManager.createListNodeByArray(nums)
            ListNodeManager.printlnNode(getKthFromEnd(head, 2))
        }

        @JvmStatic
        fun getKthFromEnd(head: ListNode?, k: Int): ListNode? {
            var fast: ListNode? = head
            var slow: ListNode? = head
            var count = k

            while (count > 0 && fast != null) {
                System.out.println(fast.`val`)
                fast = fast.next
                count--
            }

            while (fast != null && slow != null) {
                fast = fast.next
                slow = slow.next
            }
            return slow
        }
    }
}